package com.tansun.easycare.rule.rulelog.web;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import com.tansun.easycare.core.utils.ResponseTable;
import com.tansun.easycare.rule.common.RuleBaseController;
import com.tansun.easycare.rule.rulelog.entity.RuleLogs;
import com.tansun.easycare.rule.rulelog.model.FlareTreeDataEntity;
import com.tansun.easycare.rule.rulelog.service.RuleLogsService;
import com.tansun.easycare.rule.rulemange.entity.RuleManage;
import com.tansun.easycare.rule.rulemange.service.RuleManageService;
import com.tansun.easycare.rule.ruleversion.entity.RuleVersioninfo;
import com.tansun.easycare.rule.ruleversion.service.RuleVersioninfoService;
import com.tansun.rule.utils.JsonMessage;

@RestController
@RequestMapping(value = "/ruleProj/logs")
public class RuleLogController extends RuleBaseController {
	@Autowired
	private RuleLogsService ruleLogsService;
	@Autowired
	private RuleManageService ruleManageService;
	@Autowired
	private RuleVersioninfoService ruleVersioninfoService;
	
	/*
	 * 请求获取日志页面数据页面
	 */
	@RequestMapping("rulelog")
	public ResponseTable<RuleLogs> ruleindex(@RequestBody RuleLogs ruleLogs) throws Exception{
		ruleLogs.setLogType("1");
        PageInfo<RuleLogs> pageInfo = this.ruleLogsService.RulefindList(ruleLogs.getPage(),ruleLogs);
        return new  ResponseTable<RuleLogs>(pageInfo);
	}
	/*echars页面*/
	@RequestMapping("echars")
	public String releechars(HttpServletRequest request, HttpServletResponse response,
			RuleLogs ruleLogs, ModelMap map) throws Exception{
		ruleLogs.setLogType("1");
        PageInfo<RuleLogs> page = this.ruleLogsService.RulefindList(ruleLogs.getPage(),
        		ruleLogs);
        map.put("page", page);
		return "rule/rulelog/echars";
	}
	
	/*返回页面的折线数据*/
	@RequestMapping("echarsData")
	public @ResponseBody List<RuleLogs> echarsData(HttpServletRequest request, HttpServletResponse response,
			RuleLogs ruleLogs, ModelMap map) throws Exception{
		ruleLogs.setLogType("1");
        List<RuleLogs> list = this.ruleLogsService.findList(ruleLogs);
		return list;
	}
	/*点击（切换）页面*/
	@RequestMapping("groupEcharsPage")
	public String groupEcharsPage(HttpServletRequest request, HttpServletResponse response){
		return "rule/rulelog/group_echars_swith";
	}
	/*点击“切换”的饼状图数据*/
	@RequestMapping("groupEcharsPie")
	public @ResponseBody List<RuleLogs> groupEcharsPie(HttpServletRequest request, HttpServletResponse response,
			RuleLogs ruleLogs, ModelMap map) throws Exception{
		ruleLogs.setLogType("1");
        List<RuleLogs> list = this.ruleLogsService.findListSwithPie(ruleLogs);
		return list;
	}
	/*group by数据*/
	@RequestMapping("groupEchars")
	public String groupEchars(HttpServletRequest request, HttpServletResponse response,
			RuleLogs ruleLogs, ModelMap map) throws Exception{
		ruleLogs.setLogType("1");
        PageInfo<RuleLogs> page = this.ruleLogsService.RulegroupList(ruleLogs.getPage(),
        		ruleLogs);
        map.put("page", page);
		return "rule/rulelog/group_echars";
	}
	/*返回页面的饼状图数据*/
	@RequestMapping("echarsDataPie")
	public @ResponseBody List<RuleLogs> echarsDataPie(HttpServletRequest request, HttpServletResponse response,
			RuleLogs ruleLogs, ModelMap map) throws Exception{
		ruleLogs.setLogType("1");
        List<RuleLogs> list = this.ruleLogsService.groupfindListPie(ruleLogs);
		return list;
	}
	/**
	 * 执行记录挑战者信息
	 * @param ruleLogs
	 * @param map
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("challengerinfo")
	public @ResponseBody JsonMessage challengerinfo(RuleLogs ruleLogs, ModelMap map) throws Exception{
		 JsonMessage ret = new JsonMessage();
		ruleLogs.setLogType("3");
        List<RuleLogs> list = this.ruleLogsService.findList(ruleLogs);
        if(list!=null && list.size()>0) {
        	 RuleLogs logs = list.get(0);
             RuleManage ruleflow = ruleManageService.findByCode(logs.getRuleflowCode(), logs.getProjectCode());
             RuleVersioninfo condition = new RuleVersioninfo();
     		condition.setManageId(ruleflow.getId());
     		condition.setVersion(logs.getVersion());
     		List<RuleVersioninfo> verlist = ruleVersioninfoService.findList(condition);
     		if(verlist!=null && verlist.size()>0) {
     			RuleVersioninfo versioninfo = verlist.get(0);
     			String championChallenger = versioninfo.getChampionChallengerInfo();
     			List<Map> ccMapList = JSON.parseArray(championChallenger,Map.class);
     			for (Map ccmap : ccMapList) {
     				if(!ccmap.containsKey("champion") && ccmap.get("challenger")!=null) {
     					for(RuleLogs rl : list) {
     						Map<String,Object> rlMap = JSON.parseObject(rl.getRuleParam(),Map.class);
     						Map<String,String> vm = (Map<String, String>) rlMap.get("championInfo");
     						if(vm!=null) {
     							if(ccmap.get("challenger").equals(vm.get("challenger"))){
     								rl.setTestBranchName(ccmap.get("name").toString());
     							}	
     						}
     					}
     				}
     			}
     		}
     		 ret.setData((Serializable) list);
        }
        return ret;
	}
	/**
	 * 冠军挑战者数据对比界面
	 * @param champion
	 * @param challengeId
	 * @param map
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("challengerdatacompare")
	public String challengerdatacompare(String championId,String challengeId, ModelMap map) throws Exception{
		RuleLogs champion = ruleLogsService.get(championId);
		RuleLogs challenge = ruleLogsService.get(challengeId);
		RuleManage  rulemanage = ruleManageService.findByCode(champion.getRuleflowCode(), champion.getProjectCode());
		RuleVersioninfo condition = new RuleVersioninfo();
		condition.setManageId(rulemanage.getId());
		condition.setVersion(champion.getVersion());
		List<RuleVersioninfo> verlist = ruleVersioninfoService.findList(condition);
		map.put("rulemanage", rulemanage);
		map.put("versioninfo", verlist);
		map.put("champion", champion);
		map.put("challenge", challenge);
		return "rule/rulelog/challengerdatacompare";
	}
	
	@RequestMapping("getFlareTreeData")
	public @ResponseBody FlareTreeDataEntity getFlareTreeData(String logType) throws Exception{
		try {	
			FlareTreeDataEntity list = this.ruleLogsService.getFlareTreeData(logType);
			return list;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return null;
		}
	
	}
	
}
